<template>
  <div class="mainsu">
    <div class="main_spac1">
      <div class="main_white padding10 paddingBottom20">
        <div class="mainor_form">
          <div class="mainor_te">
            <el-button
              slot="reference"
              class="mainor_tea"
              @click="
                () => {
                  exportbuVisible = true;
                }
              "
              >导出</el-button
            >
          </div>
          <el-form
            :inline="true"
            :model="form"
            class="demo-form-inline"
            ref="userListFrom"
            label-width="70px"
          >
            <el-form-item label="结算单" prop="order_sn">
              <el-input
                v-model.trim="form.order_sn"  
                placeholder="结算单"
                clearable
                class="wid220"
              ></el-input>
            </el-form-item>
            <el-form-item label="订单" prop="related_order_order_sn">
              <el-input
                v-model.trim="form.related_order_order_sn"
                placeholder="订单"
                clearable
                class="wid220"
              ></el-input>
            </el-form-item>
            <el-form-item label="商户" prop="order_sn">
              <el-input
                v-model.trim="form.order_sn"
                placeholder="商户"
                clearable
                class="wid220"
              ></el-input>
            </el-form-item>
            <el-form-item label="创建日期" prop="createTime">
              <el-date-picker
                v-model="createTime"
                type="daterange"
                class="time220"
                :editable="false"
                unlink-panels
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                value-format="yyyy-MM-dd"
                :clearable="false"
                :picker-options="pickerOptions"
              />
            </el-form-item>
            <el-form-item>
              <el-button
                class="reset marginLeft10"
                type="primary"
                @click="$refs['userListFrom'].resetFields()"
                >重置</el-button
              >
              <el-button
                type="primary"
                @click="getArticleList"
                :loading="listLoading"
                >搜索</el-button
              >
            </el-form-item>
          </el-form>
        </div>
        <div class="main_num boxdq marginTop10">
          <div class="main_num_l box">
            <p>
              下单金额：
              <em>{{ list.order_money }}</em>
            </p>
            <p>
              配送金额：
              <em>{{ list.send_money }}</em>
            </p>
            <p>
              对账金额：
              <em>{{ list.account_money }}</em>
            </p>
            <p>
              差异金额：
              <em>{{ list.diff_money }}</em>
            </p>
          </div>
        </div>
        <div class="main_table marginTop10" style="min-height: 550px">
          <el-table
            ref="multipleTable"
            v-loading="listLoading"
            :data="list.list.data"
            element-loading-text="加载中"
            fit
            border
            highlight-current-row
            @selection-change="handleSelectionChange"
          >
            <el-table-column type="selection"></el-table-column>
            <el-table-column label="结算单号" prop="order_sn" />
            <el-table-column label="订单号" prop="related_order_order_sn" />
            <el-table-column
              label="商户名称"
              prop="company_name.company_name"
            />
            <el-table-column label="下单金额" prop="order_money" />
            <el-table-column label="配送金额" prop="send_money" />
            <el-table-column label="对账金额" prop="account_money" />
            <el-table-column label="差异金额" prop="diff_money" />
            <el-table-column label="制单人" prop="admin_user_name.name" />
            <el-table-column label="备注" prop="remark" />
          </el-table>
        </div>
        <div class="main_page boxdq">
          <div></div>
          <!-- 分页 -->
          <div class="main_page_r">
            <el-pagination
              :current-page="current.page"
              :page-sizes="[20, 30, 50, 100]"
              :page-size="limit"
              layout="total, sizes, prev, pager, next, jumper"
              :total="totalCount"
              background
              @size-change="changeLimit"
              @current-change="changePage"
            />
          </div>
        </div>
      </div>
    </div>
    <!-- 导出 -->
    <el-dialog
      title="导出商户结算单"
      :visible.sync="exportbuVisible"
      class="main_dialog dialog500"
    >
      <div v-if="selectArr.length == 0 ? true : false">
        <p class="exptitle">创建日期</p>
        <el-date-picker
          v-model="exportTime"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd"
          @change="datepicker"
        >
        </el-date-picker>
        <el-tooltip
          class="item"
          effect="dark"
          content="Top Center 提示文字"
          placement="top"
        >
          <i class="el-icon-question"></i>
        </el-tooltip>
      </div>
      <div v-if="selectArr.length == 0 ? true : false">
        <p class="exptitle company_name">商户名称</p>
        <el-input
          v-model="selectname"
          clearable
          placeholder="请输入内容"
        ></el-input>
      </div>
      <div v-if="selectArr.length == 0 ? false : true" class="maintip">
        你已经选择了<span>{{ selectArr.length }}</span
        >行数据
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="exportbuVisible = false">取 消</el-button>
        <el-button type="primary" @click="exportConfirm" v-repeatClick
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  settlement_index,
  export_settlement_info,
} from "@/api/financial/financialPayment";
import { apiUrl, objKeySort, Md5 } from "@/utils/public";
import dateOption from "@/options/dateOption";

export default {
  components: {},
  data() {
    return {
      // 分页
      current: {
        page: 1,
        per_page: 20,
      },
      limit: 20,
      totalCount: 1,
      // 列表数据
      list: {
        list: [],
      },
      listLoading: false,
      // 搜索
      form: {
        related_order_order_sn: "",
        order_sn: "",
        name: "",
        start_time: "",
        end_time: "",
      },
      createTime: [],
      pickerOptions: dateOption,

      // 导出
      exportbuVisible: false,
      exportTime: "",
      // 选择全部
      selectArr: [],
      selectname: "",
    };
  },
  created() {
    // 列表
    this.settlement_index();
  },
  methods: {
    // 搜索
    getArticleList() {
      this.current.page = 1;
      this.settlement_index();
    },
    // 分页
    changeLimit(pageSize) {
      this.current.per_page = pageSize;
      this.settlement_index();
    },
    changePage(page) {
      this.current.page = page;
      this.settlement_index();
    },
    // 列表
    settlement_index() {
      this.listLoading = true;
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(
              Object.assign(apiUrl().name, this.list_form(), this.current)
            ) + apiUrl().appsecret
          ),
        },
        apiUrl().name,
        this.list_form(),
        this.current
      );
      settlement_index(data).then((res) => {
        this.listLoading = false;
        if (res.errcode == 0) {
          this.list = res.data;
          this.totalCount = res.data.list.total;
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
    list_form() {
      let form = JSON.parse(JSON.stringify(this.form));
      form.start_time = this.createTime[0] ? this.createTime[0] : "";
      form.end_time = this.createTime[1] ? this.createTime[1] : "";

      return form;
    },

    // 导出时间范围
    datepicker(val) {
      let start = "";
      let end = "";
      if (val) {
        start = new Date(val[0]).getTime();
        end = new Date(val[1]).getTime();
        if (end - start > 30 * 24 * 60 * 60 * 1000) {
          this.$message.error("选择范围不能超过一个月，请重新选择");
          this.exportTime = "";
        }
      }
    },
    // 监听全选
    handleSelectionChange(val) {
      this.selectArr = [];
      val.map((item) => {
        this.selectArr.push({ id: item.id });
      });
      console.log(this.selectArr);
    },
    // 导出确认
    exportConfirm() {
      let ids = [];
      let send_time = "";
      if (this.selectArr.length > 0) {
        this.selectArr.forEach((item) => {
          ids.push(item.id);
        });
      }
      if (this.exportTime.length > 0) {
        send_time = this.exportTime.join(" ");
      }
      let form = {
        ids: ids.join("-"),
        send_time: send_time,
        name: this.selectname,
      };
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name, form)) + apiUrl().appsecret
          ),
        },
        apiUrl().name,
        form
      );
      // 导出接口
      export_settlement_info(data)
        .then((res) => {
          const link = document.createElement("a");
          let blob = new Blob([res], { type: "application/vnd.openxmlformats-  officedocument.spreadsheetml.sheet;charset=utf-8" });
          link.style.display = "none";
          link.href = URL.createObjectURL(blob);
          link.download = "财务管理结算导出单";
          document.body.appendChild(link);
          link.click();
          document.body.removeChild(link);
        })
        .catch(() => {
          this.$message.error("导出失败");
        });
    },
  },
};
</script>
<style lang="scss" scoped>
@import "@/styles/variables.scss";

.mainsu {
  .mainor_form form {
    width: calc(100% - 120px);
  }
}

// 导出订单表头
.exptitle {
  display: block;
  font-size: 16px;
  color: #000000;
  margin: 0 0 10px;
}
.company_name {
  padding-top: 15px;
}
.maintip {
  text-align: center;
  padding-top: 15px;
  font-size: 18px;
  span {
    display: inline;
    padding: 18px 0;
    color: red;
  }
}
</style>
